using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace LinqGen
{
    public static class NameDeductor
    {
        public static bool IsDictionaryTable(string tableName)
        {
            string prefix = ConfigurationManager.AppSettings["DictionaryTablePrefix"] as string;
            return (tableName.StartsWith(prefix));
        }

        public static string MapColumnNameToField(string columnName)
        {
            return (ToPascalCase(columnName));
        }

        public static string MapTableNameToClass(string tableName)
        {
            return (ToPascalCase(ExtractEntityNameFromTableName(tableName)));
        }

        public static string MapTableNameToProcedureName(string tableName)
        {
            string prefix = ConfigurationManager.AppSettings["PackagePrefix"] as string;
            string temp = prefix + ExtractEntityNameFromTableName(tableName);
            if (temp.Length > 30)
            {
                temp = temp.Substring(0, 30);
            }
            return (temp);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string ExtractEntityNameFromTableName(string tableName)
        {
            string DictionaryTablePrefix = ConfigurationManager.AppSettings["DictionaryTablePrefix"] as string;
            if (tableName.StartsWith(DictionaryTablePrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                return tableName.Substring(DictionaryTablePrefix.Length);
            }

            string TablePrefix = ConfigurationManager.AppSettings["TablePrefix"] as string;
            if (tableName.StartsWith(TablePrefix, StringComparison.InvariantCultureIgnoreCase))
            {
                return tableName.Substring(TablePrefix.Length);
            }
            return tableName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public static string ExtractFieldsNameFromParameterName(string parameterName)
        {
            return (parameterName.Substring(5));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        internal static string ToPascalCase(string text)
        {
            StringBuilder sb = new StringBuilder(text.ToLower());
            if (text.Length > 0)
            {
                // Change the first letter in uppercase
                ToUpperCase(sb, 0);

                // Iterate through the undescrores and change the letter following it to uppercase
                for (int i = 1; i < sb.Length; i++)
                {
                    char letter = sb[i];
                    if (letter == '_')
                    {
                        sb.Remove(i, 1);
                        ToUpperCase(sb, i);
                    }
                }
            }
            return (sb.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        internal static string ToCamelCase(string text)
        {
            StringBuilder sb = new StringBuilder(text.ToLower());
            if (text.Length > 0)
            {
                // Change the first letter in lowercase
                ToLowerCase(sb, 0);

                // Iterate through the undescrores and change the letter following it to uppercase
                for (int i = 1; i < sb.Length; i++)
                {
                    char letter = sb[i];
                    if (letter == '_')
                    {
                        sb.Remove(i, 1);
                        ToUpperCase(sb, i);
                    }
                }
            }
            return (sb.ToString());
        }

        private static void ToLowerCase(StringBuilder sb, int i)
        {
            if (i < sb.Length)
            {
                sb.Replace(sb[i], sb[i].ToString().ToLower()[i], i, 1);
            }
        }

        private static void ToUpperCase(StringBuilder sb, int i)
        {
            if (i < sb.Length)
            {
                sb.Replace(sb[i], sb[i].ToString().ToUpper()[0], i, 1);
            }
        }
    }
}
